import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * This class is add teacher UI
 */
public class AddTeacherUI extends JPanel{
	/**
	 *  choose teacher option
	 */
	private JComboBox teacherBox = null;
	/**
	 * array for teacher
	 */
	private ArrayList<String> l = new ArrayList<String>();
	/**
	 * chosen teacher
	 */
	private int teacherFlg;
	/**
	 * This class is add student UI
	 */
	private JButton btnDefine = null;
	/**
	 * back button 
	 */
	public JButton btnBack = null;
	/**
	 * title for the current window  
	 */
	private JLabel lbltitle = null;
	/**
	 * help button 
	 */
	public JButton btnHelp = null;
	/**
	 * course name  
	 */
	private String course;
	/**
	 * class name  
	 */
	private String cls;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	 * array for student
	 */
	private ArrayList<String> studentList;
	/**
	* Constructs 
	*/	
	public AddTeacherUI(ArrayList<String> studentList, String classFlg) {
		super();
		cls = classFlg;
		course = studentList.get(0);
		this.studentList = studentList;
		initialize();
	}

	private void initialize() {
		curPanel = this;
		
		this.add(getTeacherBox(), null);
	
		
		JLabel lblTeacher = new JLabel("Teacher:");
		lblTeacher.setBounds(new Rectangle(110, 106, 95, 25)); 
		this.add(lblTeacher);

		
		lbltitle = new JLabel("Add Techaer To class " + cls + " in course " + course);
		lbltitle.setBounds(71, 31, 300, 14);
		this.add(lbltitle);
		
		
		btnDefine = new JButton();
		btnDefine.setBounds(new Rectangle(113, 199, 195, 25));
		btnDefine.setText("Add Teacher");
		/**
		 *  add teacher Implementation
		 */
		btnDefine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				studentList.add(cls);
				studentList.add(l.get(teacherFlg));
				SQL s = new SQL(studentList,38);
				s.accept();
				if (s.getMsg().equals(true))
				{
					MsgWin msgWin = new MsgWin("This Combination of class-course-teacher","was added");
				}
				else 
				{
					 ErrWin err = new ErrWin ("This class is taking this course already in this semester ");
				}
			}
		});
		this.add(btnDefine);

		btnBack = new JButton("Back");
		btnBack.setBounds(161, 252, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		/**
		 *  help button Implementation
		 */
		btnHelp = new JButton("Help");
		btnHelp.setBounds(333, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
	
	}
	/**
	 *  chosen teacher option Implementation
	 */
	private JComboBox getTeacherBox() {
		teacherBox = new JComboBox();
		teacherBox.setBounds(new Rectangle(193, 106, 95, 25));  
		SQL s4 = new SQL(course,25);
		s4.accept();
		l = (ArrayList)s4.getMsg();
		int size = l.size();
		String tID;
			for (int i=0; i<size ; i++)
			{
		//		tID = l.get(i);
		//		SQL s5 = new SQL (tID,30);
		//		s5.accept();
		//		Object msg = s5.getMsg();
		//		System.out.println("this teacher : " + i + tID); 
				teacherBox.addItem(l.get(i));
			}
			
			teacherBox.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent e) {
				System.out.println("itemStateChanged()"); 
				teacherFlg = teacherBox.getSelectedIndex();
				System.out.println("this is : " + teacherFlg); 
			}
		});
	return teacherBox;

}


}
